/*****************************************************************************************
 * Dexter : Distributed Experimental and Tester Platform
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *****************************************************************************************/
package dexter.net;

import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.filter.LoggingFilter;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.serialization.ObjectSerializationCodecFactory;
import org.apache.mina.transport.socket.nio.SocketAcceptor;
import org.apache.mina.transport.socket.nio.SocketAcceptorConfig;

import dexter.core.host.HostAddress;
import dexter.net.msg.common.RequestListener;
import dexter.net.msg.common.BusListener;

public class Consumer {
	
    private int SERVER_PORT = 5555;
    private IoAcceptor acceptor;
    
    public Consumer(int port) {
    	SERVER_PORT = port;
        acceptor = new SocketAcceptor();
    }
    
    public Consumer(HostAddress host) {
		SERVER_PORT = host.getAddress().getPort();
        acceptor = new SocketAcceptor();
	}

	private SocketAcceptorConfig configure() {

        // Prepare the service configuration.
        SocketAcceptorConfig cfg = new SocketAcceptorConfig();
        cfg.setReuseAddress(true);
        cfg.getFilterChain().addLast(
                    "codec",
                    new ProtocolCodecFilter(
                            new ObjectSerializationCodecFactory()));
        
        //TODO: ACTIVATE DEBUG
        //cfg.getFilterChain().addLast("logger", new LoggingFilter());
        return cfg;
    }
   
    public void receive(RequestListener listener) throws IOException {

    	SocketAcceptorConfig cfg = configure();
        acceptor.bind(new InetSocketAddress(SERVER_PORT),
                new ConsumerSessionHandler(listener), cfg);

        System.out.println("Listening on port " + SERVER_PORT);
    }
    
    public void receive(BusListener listener) throws IOException {

    	SocketAcceptorConfig cfg = configure();
        acceptor.bind(new InetSocketAddress(SERVER_PORT),
                new ConsumerSessionHandler(listener), cfg);

        System.out.println("Listening on port " + SERVER_PORT);
    }
    
    public void close() {
    	acceptor.unbindAll();
    }
}
